using System;

namespace GrooveWebServicesV12Helpers
{
	/// <summary>
	/// Web services event.
	/// Applications will usually not use this class directly; instead use the event delegates in each data class.
	/// </summary>
	public class Event
	{
		private GrooveWebServicesV12Helpers.Context m_Context;

		private GrooveEventsWebService.GrooveEvents m_GrooveEvents;
		
		private string m_IdentityURL;
		private string m_CallbackURI;

		public Event(GrooveWebServicesV12Helpers.Context i_Context, string i_IdentityURL, string i_CallbackURI)
		{
			m_Context = i_Context;

			m_IdentityURL = i_IdentityURL;
			m_CallbackURI = i_CallbackURI;
		}

		#region Event Properties And Methods

        public Context Context
        {
            get { return m_Context; }
        }

		public string IdentityURL
		{
			get
			{
				return m_IdentityURL;
			}
		}

		public string CallbackURI
		{
			get
			{
				return m_CallbackURI;
			}
		}

		public static GrooveEventsWebService.GrooveEvents CreateInitializedEventsWebService(string i_IdentityURL, string i_TargetURI, GrooveWebServicesV12Helpers.Context i_Context)
		{
			//  Create the service
			GrooveEventsWebService.GrooveEvents EventsWebService = new GrooveEventsWebService.GrooveEvents();

			//  Create the Groove SOAP header
			EventsWebService.GrooveRequestHeaderValue = new GrooveEventsWebService.GrooveRequestHeader();

			//  Set appropriate header values
			EventsWebService.GrooveRequestHeaderValue.GrooveRequestKey = i_Context.RequestKey;
			EventsWebService.GrooveRequestHeaderValue.GrooveIdentityURL = i_IdentityURL;

			//  Set the URL
			EventsWebService.Url = i_Context.Host + i_TargetURI;

			//  Set Timeout
			EventsWebService.Timeout = i_Context.Timeout;

			return EventsWebService;
		}

		public GrooveEventsWebService.Event[] Read()
		{
			//  Get initialized reference to web service
			GrooveEventsWebService.GrooveEvents EventsWebService = this.GetInitializedEventsWebService(GrooveWebServicesV12Helpers.Shared.GROOVE_WEB_SERVICES_EVENTS_URI);

			System.DateTime BeginDateTime = System.DateTime.Now;

			GrooveEventsWebService.Event[] Events = EventsWebService.Read(m_CallbackURI);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("GrooveEventsWebService::Read, " + TimeSpan.TotalMilliseconds + " ms");
			}

			return Events;
		}

		public GrooveEventsWebService.ExtendedEventData ReadExtended()
		{
			return this.ReadExtended
				(
				-1,		//  Sequence
				true,	//  Include Sources
				true	//  Delete Events
				);
		}

		public GrooveEventsWebService.ExtendedEventData ReadExtended(long i_Sequence)
		{
			return this.ReadExtended
				(
				i_Sequence,
				true,		//  Include Sources
				true		//  Delete Events
				);
		}

		public GrooveEventsWebService.ExtendedEventData ReadExtended(bool i_DeleteEvents)
		{
			return this.ReadExtended
				(
				-1,				//  Sequence
				true,			//  Include Sources
				i_DeleteEvents 
				);
		}

		public GrooveEventsWebService.ExtendedEventData ReadExtended(long i_Sequence, bool i_IncludeSource)
		{
			return this.ReadExtended
				(
				i_Sequence,
				i_IncludeSource,
				true				//  Delete Events
				);
		}

		public GrooveEventsWebService.ExtendedEventData ReadExtended(long i_Sequence, bool i_IncludeSources, bool i_DeleteEvents)
		{
			//  Get initialized reference to web service
			GrooveEventsWebService.GrooveEvents EventsWebService = this.GetInitializedEventsWebService(GrooveWebServicesV12Helpers.Shared.GROOVE_WEB_SERVICES_EVENTS_URI);

			System.DateTime BeginDateTime = System.DateTime.Now;

			GrooveEventsWebService.ExtendedEventData ExtendedEventData = EventsWebService.ReadExtended
				(
				m_CallbackURI,
				i_Sequence,
				(i_Sequence != -1),		//  SequenceSpecified
				i_IncludeSources,
				true,					//  IncludeSourcesSpecified
				i_DeleteEvents,			
				true					//  DeleteEventsSpecified
				);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("GrooveEventsWebService::ReadExtended, " + TimeSpan.TotalMilliseconds + " ms");
			}

			return ExtendedEventData;
		}

		public GrooveEventsWebService.EventQueueSummary ReadSummary()
		{
			return this.ReadSummary(true);
		}

		public GrooveEventsWebService.EventQueueSummary ReadSummary(bool i_IncludeSources)
		{
			//  Get initialized reference to web service
			GrooveEventsWebService.GrooveEvents EventsWebService = this.GetInitializedEventsWebService(GrooveWebServicesV12Helpers.Shared.GROOVE_WEB_SERVICES_EVENTS_URI);

			System.DateTime BeginDateTime = System.DateTime.Now;

			GrooveEventsWebService.EventQueueSummary EventQueueSummary = EventsWebService.ReadSummary
				(
				m_CallbackURI,
				i_IncludeSources
				);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("GrooveEventsWebService::ReadSummary, " + TimeSpan.TotalMilliseconds + " ms");
			}

			return EventQueueSummary;
		}

		public long Delete(long i_Sequence)
		{
			//  Get initialized reference to web service
			GrooveEventsWebService.GrooveEvents EventsWebService = this.GetInitializedEventsWebService(GrooveWebServicesV12Helpers.Shared.GROOVE_WEB_SERVICES_EVENTS_URI);

			System.DateTime BeginDateTime = System.DateTime.Now;

			long DeleteResponse = EventsWebService.Delete
				(
				m_CallbackURI,
				i_Sequence
				);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("GrooveEventsWebService::Delete, " + TimeSpan.TotalMilliseconds + " ms");
			}

			return DeleteResponse;
		}

		private GrooveEventsWebService.GrooveEvents GetInitializedEventsWebService(string i_TargetURI)
		{
            if ((m_GrooveEvents == null) || Context.ReinitNeeded(m_GrooveEvents.GrooveRequestHeaderValue.GrooveRequestKey))
			{
				m_GrooveEvents = GrooveWebServicesV12Helpers.Event.CreateInitializedEventsWebService
					(
					m_IdentityURL,
					i_TargetURI,
					Context
					);
			}
			else
			{
				//  Explicitly overwrite target URL and Timeout
				m_GrooveEvents.Url = Context.Host + i_TargetURI;
                m_GrooveEvents.Timeout = Context.Timeout;
			}

			return m_GrooveEvents;
		}

	}
	#endregion
}
